/*
 * Config.cpp
 *
 *  Created on: Jul 17, 2013
 *      Author: Dan
 */

#include "Config.h"

namespace sm {

const std::string PREFIX = "tns";

Config::Config(monkeyXML::XMLTree& aTree) throw (monkeyXML::parseException) {
	Log::event("Reading in configurations");
	if (aTree.getTag().substr(3, 7) == ":config") {
		monkeyXML::XMLTree* t;
		for (int i = 0; i < aTree.childCount(); i++) {
			t = aTree.getNextChild();
			//Go through setting settings.
			//Debug Mode
			if (t->getTag() == PREFIX + ":debug") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Debug is " + child->getTag());
				if (child->getTag() == "true" || child->getTag() == "True"
						|| child->getTag() == "TRUE") {
					this->isDebugMode = true;
				} else if (child->getTag() == "false"
						|| child->getTag() == "False"
						|| child->getTag() == "FALSE") {
					this->isDebugMode = false;
				} else {
					Log::error("Set debug to true or false. Case sensitive.\n");
					monkeyXML::parseException e;
					throw e;
				}

				//LogFile
			} else if (t->getTag().substr(3, 8) == ":logfile") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("LogFile is " + child->getTag());
				this->theLogFile = child->getTag();

				//Debug Log File
			} else if (t->getTag().substr(3, 10) == ":debugfile") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Debug File is " + child->getTag());
				this->theDebugLogFile = child->getTag();

				//Input Directory
			} else if (t->getTag().substr(3, 9) == ":inputdir") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Input Directory is " + child->getTag());
				this->theInputDir = child->getTag();

				//Response Directory
			} else if (t->getTag().substr(3, 12) == ":responsedir") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Response Directory is " + child->getTag());
				this->theResponseDir = child->getTag();

				// Unresolved Turns Directory
			} else if (t->getTag().substr(3, 9) == ":turnsdir") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Turns Directory is " + child->getTag());
				this->theTurnsDir = child->getTag();

				// Games List File
			} else if (t->getTag().substr(3, 10) == ":gamesfile") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Games File is " + child->getTag());
				this->theGamesFile = child->getTag();

				// Player List File
			} else if (t->getTag().substr(3, 12) == ":playersfile") {
				monkeyXML::XMLTree* child = t->getNextChild();
				Log::event("Players File is " + child->getTag());
				this->thePlayerFile = child->getTag();

				//Unknown tag
			} else {
				Log::error("Unknown Tag in configurations XML");
				monkeyXML::parseException e;
				throw e;
			}
		}
		aTree.writeToStream(std::cout);
	} else {
		Log::error(
				"Root element is " + aTree.getTag() + ". Should be " + PREFIX
						+ ":config.");
		throw(monkeyXML::parseException());
	}
}

Config::~Config() {
	// TODO Auto-generated destructor stub
}

} /* namespace monkeyXML */
